package cn.com.gridinfo.sooc.core.my.msg.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.my.msg.adapter.DiscussMsgAdapter;
import cn.com.gridinfo.sooc.core.my.msg.bean.DiscussInfo;
import cn.com.gridinfo.sooc.core.my.msg.common.CodeConst;
import cn.com.gridinfo.sooc.core.my.msg.dao.MsgDao;
import cn.com.gridinfo.sooc.core.my.msg.listener.MyItemLongClickListener;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.MyMaterialDialog;

/**
 * Created by Michelle on 2016/1/6.
 * 消息讨论
 */
public class DiscussMsgActivity extends BaseSoocActivity implements MyItemLongClickListener {
    @InjectView(R.id.msg_discuss_listView)
    UltimateRecyclerView discussRecycleView;
    private MsgDao dao;
    DiscussMsgAdapter adapter;//适配器
    LinearLayoutManager linearLayoutManager;//列表管理器
    private Context context = DiscussMsgActivity.this;//上下文对象
    int pageNo = 1;//页数
    int pageSize = 10;//每页的条数
    int maxLastPositon;//数据最后位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_msg_activity_discuss);
        ButterKnife.inject(this);
        dao = new MsgDao(this);
        dao.getDiscuss(pageNo, pageSize, CodeConst.DISCUSS_FIRST);
        linearLayoutManager = new LinearLayoutManager(context);
        discussRecycleView.setLayoutManager(linearLayoutManager);
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "讨论通知";
    }

    //设置左边按钮
    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //点击课程分类
    @OnClick(R.id.toolbar_leftbtn)
    void goBack() {
        finish();
    }

    public void deleteItem(String id) {
        dao.deleteDiscuss(id);
    }


    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        try {
            if (dao.status.equals("0")) {
                switch (requestCode) {
                    //课程首次请求
                    case CodeConst.DISCUSS_FIRST:
                        loadMessage();
                        break;
                    //课程刷新操作
                    case CodeConst.DISCUSS_PUSHDOWN:
                        discussRecycleView.setRefreshing(false);
                        if (dao.getDiscussList() != null && !dao.getDiscussList().isEmpty()) {
                            discussRecycleView.hideEmptyView();
                        } else {
                            //无数据处理
                            discussRecycleView.showEmptyView();
                        }
                        //刷新数据
                        adapter.refresh(dao.getDiscussList());
                        if (dao.getDiscussList().size() > (pageSize - 1))
                            //数据大于每页显示的条数，则重新开启加载更多
                            adapter.loadMoreDataView();
                        else {
                            //数据小于当前页的话则不加载显示更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(0);
                        }
                        break;
                    //课程翻页操作
                    case CodeConst.DISCUSS_PULLUP:
                        if (dao.getDiscussList() != null && !dao.getDiscussList().isEmpty()) {
                            //加载更多
                            adapter.loadmore(dao.getDiscussList());
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                    case CodeConst.DELETE_DISCUSS:
                        adapter.deleteSuccess();
                        MessageUtils.showImageShortToast(context, dao.statusCode);
                }
            } else {
                MessageUtils.showImageShortToast(context, dao.statusCode);
            }

        } catch (Exception e) {
            MessageUtils.showImageShortToast(context, "请求数据异常");
        }
    }

    //初始化list数据 分页
    private void loadMessage() {
        if (dao.getDiscussList().isEmpty()) {
            discussRecycleView.showEmptyView();
        } else {
            Log.d("==============分页数据f=================");
            adapter = new DiscussMsgAdapter(context, dao.getDiscussList());
            discussRecycleView.setAdapter(adapter);
            adapter.setOnItemLongClickListener(this);
            discussRecycleView.enableLoadmore();
            discussRecycleView.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    pageNo = 1;//首页
                    //加载首页数据
                    dao.getDiscuss(pageNo, pageSize, CodeConst.DISCUSS_PUSHDOWN);
                    linearLayoutManager.scrollToPosition(0);
                }
            });
            discussRecycleView.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
                @Override
                public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                    if (itemsCount > (pageSize - 1)) {
                        maxLastPositon = maxLastVisiblePosition;
                        //加载下一页数据
                        dao.getDiscuss(++pageNo, pageSize, CodeConst.DISCUSS_PULLUP);
                    } else {
                        adapter.loadNoDataView();
                        linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                    }
                }
            });
        }
    }

    @Override
    public void onRequestFaild(int requestCode, String errorNo, String errorMessage) {
        super.onRequestFaild(requestCode, errorNo, errorMessage);
        MessageUtils.showImageShortToast(context, dao.statusCode);
    }

    @Override
    public void onItemLongClick(View view, int position) {
//        final DiscussInfo bean = adapter.getData().get(position);
//        if(bean != null){
//            MyMaterialDialog.showMaterialDialog(context, "删除通知", "确定要删除这条讨论通知吗？"
//                    , "取消", new MaterialDialog.SingleButtonCallback() {
//                @Override
//                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                    dialog.dismiss();
//                }
//            }, "确定", new MaterialDialog.SingleButtonCallback() {
//                @Override
//                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                    deleteItem(bean.getTlid().toString());
//                }
//            });
//        }
    }
}
